//
//  KFDGetCheckcodeTableViewController.m
//  KFD
//
//  Created by JIRENTIANXIANG on 7/1/15.
//  Copyright © 2015 kfd. All rights reserved.
//

#import "KFDGetCheckcodeTableViewController.h"
#import "KFDUIConfigurator.h"
#import "UIButton+KFD.h"
#import "ui_config.h"
#import "KFDAccountManager.h"
#import "JTToast.h"
#import "KFDUpdatePasswordViewController.h"

@interface KFDGetCheckcodeTableViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;
@property (weak, nonatomic) IBOutlet UITextField *checkcodeTextField;
@property (weak, nonatomic) IBOutlet UIView *footerView;
@property (weak, nonatomic) IBOutlet UIButton *checkcodeButton;
@property (strong,nonatomic) UIButton* updatePasswordButton;

@property (strong,nonatomic) NSTimer* timer;

@end

@implementation KFDGetCheckcodeTableViewController
{
    int _countdown;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    KFDUIConfigurator* uiConfigurator=[KFDUIConfigurator configuratorForViewController:self];
    [uiConfigurator configureNavigationBarWithBackAction:@selector(goBack)];
    
    self.updatePasswordButton=[UIButton redButtonWithtitle:@"填写新密码"];
    self.updatePasswordButton.center=CGPointMake(SCREEN_WIDTH/2, 60);
    [self.footerView addSubview:self.updatePasswordButton];
    [self.updatePasswordButton addTarget:self action:@selector(updatePasswordButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    _countdown=60;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)checkcodeButtonPressed:(id)sender {
    NSString* phoneNumber=self.phoneNumberTextField.text;
    KFDAccountManager* manager=[KFDAccountManager sharedManager];
    if ([manager checkPhoneNumber:phoneNumber illegalInputHandler:^(NSString *message) {
        [[JTToast makeText:message] showInView:self.view size:CGSizeMake(message.length*20, 40)];
    }]) {
        [manager getCheckcodeForChangingPasswordWithPhoneNumber:phoneNumber successHandler:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                self.checkcodeButton.userInteractionEnabled=NO;
                [self.checkcodeButton setTitle:@"60秒" forState:UIControlStateNormal];
                self.timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countdown) userInfo:nil repeats:YES];
            });
        } failureHandler:^(KFDResponseBody *response) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [[JTToast makeText:response.message] showInView:self.view size:CGSizeMake(response.message.length*20, 40)];
            });
        }];
    }
}

-(void)countdown
{
    _countdown-=1;
    [self.checkcodeButton setTitle:[NSString stringWithFormat:@"%d秒",_countdown] forState:UIControlStateNormal];
    if (_countdown==0) {
        _countdown=60;
        [self.timer invalidate];
        self.checkcodeButton.userInteractionEnabled=YES;
        [self.checkcodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    }
}

-(void)updatePasswordButtonPressed
{
    [self.phoneNumberTextField resignFirstResponder];
    [self.checkcodeTextField resignFirstResponder];
    KFDAccountManager* manager=[KFDAccountManager sharedManager];
    NSString* checkcode=self.checkcodeTextField.text;
    if ([manager checkCheckcode:checkcode illegalInputHandler:^(NSString *message) {
        [[JTToast makeText:message] showInView:self.view size:CGSizeMake(message.length*20, 40)];
    }]) {
        [manager verifyCheckcode:checkcode withType:@"change" phoneNumber:self.phoneNumberTextField.text successHandler:^(NSString *token) {
            dispatch_async(dispatch_get_main_queue(), ^{
                KFDUpdatePasswordViewController* updatePasswordVC=[[UIStoryboard storyboardWithName:@"UpdatePassword" bundle:nil] instantiateViewControllerWithIdentifier:@"KFDUpdatePasswordViewController"];
                updatePasswordVC.token=token;
                [self.navigationController pushViewController:updatePasswordVC animated:YES];
            });
        } failureHandler:^(KFDResponseBody *response) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [[JTToast makeText:response.message] showInView:self.view size:CGSizeMake(response.message.length*20, 40)];
            });
        }];
    }
}

@end
